<?php

session_start();

class Functiontest_generate_family_members extends CI_Controller {

	public function __construct() {
            parent::__construct(); 

	}
        
        public function generate_male_name() {
            $query = $this->doctrine->em->createQuery("select max(u.id) from ORM\Dynasties2\Malenames u");
             $result = $query->getSingleResult();
             //echo $result2[1];
             $highval = $result[1];
             $random_name = rand(1,$highval);
             //echo $random_name;
             $name = $this->doctrine->em->find('ORM\Dynasties2\Malenames', $random_name);
             return $name->getName();
            
            
        }
        
        public function generate_female_name() {
            $query = $this->doctrine->em->createQuery("select max(u.id) from ORM\Dynasties2\Femalenames u");
             $result = $query->getSingleResult();
             //echo $result2[1];
             $highval = $result[1];
             $random_name = rand(1,$highval);
             //echo $random_name;
             $name = $this->doctrine->em->find('ORM\Dynasties2\Femalenames', $random_name);
             return $name->getName();
        }
        


        public function generate_family_members(){   /// change this back to private!!!
             echo "</br> Function (private) generate_family_members()  TEST</br>";
             //
             // need security check so that this function cannot be run without permission ... seems to be ok since making this a private function
             
             // need to know current turn
             $current_turn = '1';
             
             // determine birth turn for 'main' character,  should be no younger than 35 years(280), no older than 40 years (320)
             $age_rand = rand(280,320);
             $born_turn = $current_turn - $age_rand;
             //echo $born_turn;
             
             // great, his parents must have been married before this date.  lets figure 6 to 12 months before.
             $marriage_rand = rand(7,12);
             $parents_marriage_turn = $born_turn - $marriage_rand;
             // turn the parents were married ^^
             
             // how old are his parents?  Dad needs to be around 20 when married, give or take, and mom similar though both can vary some.
             // dad never younger than 19 and mom never younger than 18, dad never older than 24 mom never older than 24
             $dadage_atmarriage = rand(152,192);
             $momage_atmarriage = rand(144,192);

             $dad_born_turn = $parents_marriage_turn - $dadage_atmarriage;
             $mom_born_turn = $parents_marriage_turn - $momage_atmarriage;
             //echo $dad_born_turn;
             //echo $mom_born_turn;
             
             $dadage_years = ($current_turn - $dad_born_turn) / 8;
             //echo $dadage_years;
             
             /////////////
             ////// this is terrible random name code.  surely there is a way to do it better
             ////
             $father_name = $this->generate_male_name();
             /// end awful random name code
             
             $dad_died_turns_ago = rand(1,8);
             $dad_dead_turn = $current_turn - $dad_died_turns_ago;
             
             $character_data['name'] = $father_name;
             $character_data['whichfamily'] = $_SESSION['active_family'];
             $character_data['bornturn'] = $dad_born_turn;
             $character_data['deathturn'] = $dad_dead_turn;
             $character_data['marriedturn'] = $parents_marriage_turn;
             $character_data['marriedto'] = '0'; // need to get spouse ID
             $character_data['isfemale'] = '0';
             $character_data['fathersID'] = '0';
             $character_data['religion'] = '0';
             $character_data['successionorder'] = '0';
             
             $this->load->model('dynasties2/setup_data_model');
             /// how do I get the returned results from this query right here????
             $dad_id = $this->setup_data_model->commit_new_character_to_db($character_data);
             /// answer... this ^^^
             
             //echo "new char ID is " . $dad_id;
             //echo $dad_id;
             //  ^^^^ dad created ^^^^
             
             /////////////
             ////// this is terrible random name code.  surely there is a way to do it better
             ////    probably needs to be a library for the time being
             ////
             $mother_name = $this->generate_female_name();
             /// end awful random name code
             
             $character_data['name'] = $mother_name;
             $character_data['whichfamily'] = $_SESSION['active_family'];
             $character_data['bornturn'] = $mom_born_turn;
             $character_data['deathturn'] = '0';  // we are assuming mom is still alive
             $character_data['marriedturn'] = $parents_marriage_turn;
             $character_data['marriedto'] = $dad_id; // need to get spouse ID
             $character_data['isfemale'] = '1';
             $character_data['fathersID'] = '0';
             $character_data['religion'] = '0';
             $character_data['successionorder'] = '0';
             
             // unneccessary? $this->load->model('dynasties2/setup_data_model');
             $mom_id = $this->setup_data_model->commit_new_character_to_db($character_data);
             //echo $mom_id;  // need to use this to update husband's marriedto value
             $husband = $this->doctrine->em->find('ORM\Dynasties2\Characters', $dad_id);
             $husband->setMarriedTo($mom_id);
             $this->doctrine->em->persist($husband);
             $this->doctrine->em->flush();
             //  ^^^^ mom created ^^^^
             
             ////////// begin create 'main character' /////////////
             
             /// note that this currently assumes that the main character is male!
             
             /// awful random name code
             $my_name = $this->generate_male_name();
             /// end awful random name code
             
             $character_data['name'] = $my_name;
             $character_data['whichfamily'] = $_SESSION['active_family'];
             $character_data['bornturn'] = $born_turn; /// generated way above
             $character_data['deathturn'] = '0';  
             $character_data['marriedturn'] = '0';  // not married?
             $character_data['marriedto'] = '0';  // not married?
             $character_data['isfemale'] = '0';
             $character_data['fathersID'] = $dad_id;
             $character_data['religion'] = '0';
             $character_data['successionorder'] = '1';   ///  <-- very important!
             
             $my_id = $this->setup_data_model->commit_new_character_to_db($character_data);
             
             /// need to create a family tree table to track mother as well..
             
             ////////////////// end creation of main character ////
             
             //////// create a wife!  //////
             
             $agerand = rand(264,304);
             $wife_born_turn = $current_turn - $agerand;  // turn that this person was born
             
             $marriagerand = rand(120,144);
             $married_turn = $current_turn - $marriagerand;
             
             /// awful random name code
             $wife_name = $this->generate_female_name();
             
             $character_data['name'] = $wife_name;
             $character_data['whichfamily'] = $_SESSION['active_family'];
             $character_data['bornturn'] = $wife_born_turn; /// generated way above
             $character_data['deathturn'] = '0';  
             $character_data['marriedturn'] = $married_turn;  // not married?
             $character_data['marriedto'] = $my_id;  // not married?
             $character_data['isfemale'] = '1';
             $character_data['fathersID'] = '0';
             $character_data['religion'] = '0';
             $character_data['successionorder'] = '0';
             
             $wife_id = $this->setup_data_model->commit_new_character_to_db($character_data);
             
             $my_char = $this->doctrine->em->find('ORM\Dynasties2\Characters', $my_id);
             $my_char->setMarriedTo($wife_id);
             $my_char->setMarriedDate($married_turn);
             $this->doctrine->em->persist($my_char);
             $this->doctrine->em->flush();
             
             ///////// end of create wife
             
             ////////// begin children creation code /////////////////
             // twins?
             // bastards?
             //
             /// create some children
             
             
        
                // how many?
                $hmany = rand(1,100);
                if ($hmany > '0' && $hmany < '31') {
                    // 3 living children
                    $children = '3';
                } else if ($hmany > '30' && $hmany < '51') {
                    // 4 living
                    $children = '4';
                } else if ($hmany > '50' && $hmany < '81') {
                    // 5 living
                    $children = '5';
                } else if ($hmany > '80' && $hmany < '101') {
                    // 6 living
                    $children = '6';
                }

                $crand = rand(7,24);

                $lastbirth = $married_turn + $crand;


                //$boys = '0';
                //$girls = '0';
                $succession = '2';
                while ($children > '0') {
                    $sex = rand(1,2);
                    if ($sex == '1') {
                        //$boys++;

                        /// awful random name code
                        $boy_name = $this->generate_male_name();

                        $bornafter = rand(7,24);
                        $birthday = $lastbirth + $bornafter;


                        if ($birthday <= $current_turn) {
                            
                            $character_data['name'] = $boy_name;
                            $character_data['whichfamily'] = $_SESSION['active_family'];
                            $character_data['bornturn'] = $birthday;
                            $character_data['deathturn'] = '0';  
                            $character_data['marriedturn'] = '0';  
                            $character_data['marriedto'] = '0'; 
                            $character_data['isfemale'] = '0';
                            $character_data['fathersID'] = $my_id;
                            $character_data['religion'] = '0';
                            $character_data['successionorder'] = $succession;
                            
                            $this->setup_data_model->commit_new_character_to_db($character_data);
                            $succession++;
                            /*
                                    /// makes sure that we don't have children with borndates later than the current turn
                                mysql_query("INSERT INTO characters (name, whichfamily, borndate, leaderskill, mgmtskill, spyskill, religion, location, succession, fathersID) 
                            VALUES ('$name', $famID, $birthday, '55', '55', '55', '1', '1', '$succession', '$mcharID')");
                                $charID = mysql_insert_id();
                                $succession++;

                                mysql_query("INSERT INTO familytree (charID, father, mother) 
                            VALUES ($charID, $mcharID, $wifeID)");

                                //echo "<br>Son created.</br>";
                             * 
                             */
                        } // no else!


                        } else {
                            //$girls++;

                            /// awful random name code
                            $girl_name = $this->generate_female_name();

                            $bornafter = rand(7,24);
                            $birthday = $lastbirth + $bornafter;

                            if ($birthday <= $current_turn) {
                                /// makes sure that we don't have children with borndates later than the current turn
                                
                                $character_data['name'] = $girl_name;
                                $character_data['whichfamily'] = $_SESSION['active_family'];
                                $character_data['bornturn'] = $birthday; 
                                $character_data['deathturn'] = '0';  
                                $character_data['marriedturn'] = '0'; 
                                $character_data['marriedto'] = '0'; 
                                $character_data['isfemale'] = '1';
                                $character_data['fathersID'] = $my_id;
                                $character_data['religion'] = '0';
                                $character_data['successionorder'] = '0';  // the system is sexist at the moment
                                
                                $this->setup_data_model->commit_new_character_to_db($character_data);
                                $succession++;
                                
                                /*
                                    mysql_query("INSERT INTO characters (name, whichfamily, borndate, leaderskill, mgmtskill, spyskill, religion, location, isfemale, fathersID) 
                                VALUES ('$name', $famID, $birthday, '55', '55', '55', '1', '1', '1', '$mcharID')");
                                    $charID = mysql_insert_id();

                                    mysql_query("INSERT INTO familytree (charID, father, mother) 
                                VALUES ($charID, $famID, $wifeID)");
                                    //echo "<br>Daughter created.</br>";
                                 * 
                                 */
                            } 

                        } // no else!


                    $lastbirth = $birthday;
                    $children--;
                }
             //////////// end of children creation code
             
                
             
             
             
         } ////////// end of function create_family_members
        ////////////////////////////////////////////////
         
} /// end of class Functiontest_generate_family_members extends CI_Controller {